Skip to content

Commit 5c0147d

Browse files
committed
add new filters
- Customize directory where the products's feed is written - Customize products's feed filename - Customize shipping method used when creating the shipping rate during order import
1 parent 605cbaf commit 5c0147d

5 files changed

Lines changed: 138 additions & 6 deletions

File tree

src/Feed/FeedBuilder/FeedBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ public function render_feed( $lang = null ): void {
164164
* @return bool|\WP_Error
165165
*/
166166
protected function write_products_feed( string $file_path, array $products ) {
167+
168+
/**
169+
* Filter the list of products to be written to the feed.
170+
*
171+
* @param \WC_Product[] $products
172+
*/
173+
$products = apply_filters( 'shopping_feed_products_for_feed', $products );
174+
167175
$products_list = Products::get_instance()->format_products( $products );
168176
try {
169177
$generator = Generator::make( $file_path );

src/Orders/Order/Products.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ private function mapping_product( $sf_product, $references_aliases = [] ) {
8282
$wc_product_id = $references_aliases[ $sf_product->getReference() ] ?? $sf_product->getReference();
8383

8484
if ( 'sku' === $product_identifier ) {
85+
86+
/**
87+
* Filter the SKU from the ShoppingFeed order.
88+
*
89+
* @param string $wc_product_id
90+
* @param OrderItem $sf_product
91+
* @param array $references_aliases
92+
*/
93+
$wc_product_id = apply_filters( 'shopping_feed_order_products_product_sku', $wc_product_id, $sf_product, references_aliases );
94+
8595
$wc_product_id = wc_get_product_id_by_sku( $wc_product_id );
8696
if ( ! $wc_product_id ) {
8797
return array();

src/Orders/Order/Shipping.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ private function set_shipping_rate() {
9999
return;
100100
}
101101
$shipping_rate = ShoppingFeedHelper::get_wc_shipping_from_sf_carrier( $this->method );
102+
103+
/**
104+
* Filter the shipping method data used when creating the shipping rates.
105+
*
106+
* @param array $shipping_rate the shipping method data.
107+
* @param OrderResource $sf_order the ShoppingFeed order.
108+
*/
109+
$shipping_rate = apply_filters( 'shopping_feed_order_shipping_rate', $shipping_rate, $this->sf_order );
110+
102111
if ( empty( $shipping_rate ) ) {
103112
$shipping_rate = $default_shipping_method;
104113
}

src/ShoppingFeedHelper.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,49 @@ public static function get_wc_version() {
5656
}
5757

5858
/**
59-
* Return the feed's directory
59+
* Return the feed's directory.
60+
*
6061
* @return string
6162
*/
6263
public static function get_feed_directory() {
63-
return SF_FEED_DIR;
64+
/**
65+
* Filter the path to the directory where product feeds are stored.
66+
*
67+
* @param string $path Path to the directory.
68+
*/
69+
return (string) apply_filters( 'shopping_feed_feed_directory_path', SF_FEED_DIR );
6470
}
6571

6672
/**
67-
* Return the feed's parts directory
73+
* Return the feed's parts directory.
74+
*
6875
* @return string
6976
*/
7077
public static function get_feed_parts_directory() {
71-
return SF_FEED_PARTS_DIR;
78+
/**
79+
* Filter the path to the directory where product feeds parts are stored.
80+
*
81+
* @param string $path Path to the directory.
82+
*/
83+
return (string) apply_filters( 'shopping_feed_feed_parts_directory_path', SF_FEED_PARTS_DIR );
7284
}
7385

7486
/**
75-
* Return the feed's file name
87+
* Return the feed's file name.
88+
*
89+
* The filename doesn't contain the file extension.
90+
*
7691
* @return string
7792
*/
7893
public static function get_feed_filename() {
79-
return 'products';
94+
/**
95+
* Filter the product feed's filename.
96+
*
97+
* The filename must not contain the file extension.
98+
*
99+
* @param string $path Feed's filename.
100+
*/
101+
return (string) apply_filters( 'shopping_feed_feed_filename', 'products' );
80102
}
81103

82104
/**
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace ShoppingFeed\ShoppingFeedWC\Tests\wpunit\Feed;
4+
5+
use ShoppingFeed\ShoppingFeedWC\ShoppingFeedHelper;
6+
7+
class HelperFunctionsTest extends \Codeception\TestCase\WPTestCase {
8+
9+
private static $upload_dir;
10+
11+
public static function setUpBeforeClass(): void {
12+
self::$upload_dir = wp_upload_dir();
13+
}
14+
15+
public function test_get_feed_directory() {
16+
$this->assertEquals(
17+
self::$upload_dir['basedir'] . '/shopping-feed',
18+
ShoppingFeedHelper::get_feed_directory()
19+
);
20+
}
21+
22+
public function test_get_feed_directory_filter() {
23+
$custom_feed_directory = '/tmp/shopping-feed';
24+
25+
add_filter(
26+
'shopping_feed_feed_directory_path',
27+
function ( $dir ) use ( $custom_feed_directory ) {
28+
return $custom_feed_directory;
29+
}
30+
);
31+
32+
$this->assertEquals(
33+
$custom_feed_directory,
34+
ShoppingFeedHelper::get_feed_directory()
35+
);
36+
}
37+
38+
public function test_get_feed_part_directory() {
39+
$this->assertEquals(
40+
self::$upload_dir['basedir'] . '/shopping-feed/parts',
41+
ShoppingFeedHelper::get_feed_parts_directory()
42+
);
43+
}
44+
45+
public function test_get_feed_part_directory_filter() {
46+
$custom_feed_directory = '/tmp/shopping-feed-parts';
47+
48+
add_filter(
49+
'shopping_feed_feed_parts_directory_path',
50+
function ( $dir ) use ( $custom_feed_directory ) {
51+
return $custom_feed_directory;
52+
}
53+
);
54+
55+
$this->assertEquals(
56+
$custom_feed_directory,
57+
ShoppingFeedHelper::get_feed_parts_directory()
58+
);
59+
}
60+
61+
public function test_get_feed_filename() {
62+
$this->assertEquals(
63+
'products',
64+
ShoppingFeedHelper::get_feed_filename()
65+
);
66+
}
67+
68+
public function test_get_feed_filename_filter() {
69+
$custom_feed_filename = 'custom-products';
70+
71+
add_filter(
72+
'shopping_feed_feed_filename',
73+
function ( $filename ) use ( $custom_feed_filename ) {
74+
return $custom_feed_filename;
75+
}
76+
);
77+
78+
$this->assertEquals(
79+
$custom_feed_filename,
80+
ShoppingFeedHelper::get_feed_filename()
81+
);
82+
}
83+
}

0 commit comments

Comments
 (0)